<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/31
 * Time: 17:18
 */

namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;

class HelpArticle extends Adminbase
{

    /**
     * 帮扶文章
     * */
    public function index()
    {

        if($this->request->isAjax()){
            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 10);
            $where = [];
            $order = "id DESC";

            $where[] = ['status','=',1];
            $where[] = ['type','<',3];

            $title = input('title');
            if($title){
                $where[] = ['title','like','%'.$title.'%'];
            }
            $time = input('time');
            if($time){
                $start_time = substr($time, 0, 20);//订单那创建开始时间
                $end_time = substr($time, 21, 20);//订单那创建开始时间
                $time = strtotime($start_time);
                $times = strtotime($end_time);
                $where[] = ['create_time','between',$time.','.$times];
            }

            $list = Db::name('article')
                ->where($where)
                ->page($page,$limit)
                ->order($order)
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->withAttr('type', function ($value, $data) {
                    if($value == 1){
                        return "帮扶文章";
                    }else{
                        return "--";
                    }
                })
                ->withAttr('create_user', function ($value, $data) {
                    if($value){
                        $user = Db::name('admin')->where(['userid' => $value])->value('nickname');
                        if($user){
                            return $user;
                        }else{
                            return "--";
                        }
                    }else{
                        return "--";
                    }
                })
                ->select();

            $total = Db::name('article')
                ->where($where)
                ->order($order)
                ->count();
            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);

        }
        return $this->fetch();

    }


    /**
     *
     * 文章置顶修改
     *
     * */
    public function setstate()
    {

        if($this->request->isPost()){

            $id = $this->request->param('id/d');
            empty($id) && $this->error('参数不能为空！');
            $isIndex = $this->request->param('isIndex/s') === 'true' ? 1 : 0;

            $data['is_index'] = $isIndex;
            $res = Db::name('article')->where(['id' => $id])->update($data);

            $res != false ? true : false;

            if ($res) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }

        }

    }


    /**
     * 文章添加/修改页面
     * */
    public function edit()
    {

        if($this->request->isPost()){

            $data = input('post.');
            $isTop = input('isTop','off');

            if(empty($data['id'])){
                $add = true;
            }else{
                $id = $data['id'];
                $add = false;
            }
            unset($data['id']);

            if($isTop && $isTop == "on"){
                $isTop = 1;
            }else{
                $isTop = 0;
            }

            $data['isTop'] = $isTop;
            if($add){
                $data['create_user'] = $_SESSION['think']['admin_user_auth']['uid'];
                $data['create_time'] =time();
                $res = Db::name('article')->insert($data);
            }else{
                $res = Db::name('article')->where(['id' => $id])->update($data);
            }

            if($res){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }

        }else{

            $id = $this->request->param('id/d');

            if($id){

                $data = Db::name('article')->where(['id' => $id])->find();
                if($data['pic']){
                    $data['pic'] = "/uploads/images/article/".$data['pic'];
                }
            }else{
                $data = [
                    'id' => '',
                    'title' => '',
                    'type' => '',
                    'info' => '',
                    'create_user' => '',
                    'create_time' => '',
                    'sort' => '',
                    'is_index' => '',
                    'pic' => '',
                    'isTop' => '',
                    'text' => ''
                ];
            }
            $this->assign('data',$data);

            return $this->fetch();
        }

    }


    /**
     * 青年/妇女创业
     * */
    public function women()
    {

        if($this->request->isAjax()){
            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 10);
            $where = [];
            $order = "id DESC";

            $where[] = ['status','=',1];
            $where[] = ['type','=',3];

            $title = input('title');
            if($title){
                $where[] = ['title','like','%'.$title.'%'];
            }
            $time = input('time');
            if($time){
                $start_time = substr($time, 0, 20);//订单那创建开始时间
                $end_time = substr($time, 21, 20);//订单那创建开始时间
                $time = strtotime($start_time);
                $times = strtotime($end_time);
                $where[] = ['create_time','between',$time.','.$times];
            }

            $list = Db::name('article')
                ->where($where)
                ->page($page,$limit)
                ->order($order)
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->withAttr('type', function ($value, $data) {
                    if($value == 1){
                        return "帮扶文章";
                    }else{
                        return "--";
                    }
                })
                ->withAttr('create_user', function ($value, $data) {
                    if($value){
                        $user = Db::name('admin')->where(['userid' => $value])->value('nickname');
                        if($user){
                            return $user;
                        }else{
                            return "--";
                        }
                    }else{
                        return "--";
                    }
                })
                ->select();

            $total = Db::name('article')
                ->where($where)
                ->order($order)
                ->count();
            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);

        }
        return $this->fetch();

    }


    /**
     * 青年/妇女创业添加/修改页面
     * */
    public function edits()
    {

        if($this->request->isPost()){

            $data = input('post.');
            $isTop = input('isTop','off');

            if(empty($data['id'])){
                $add = true;
            }else{
                $id = $data['id'];
                unset($data['id']);
                $add = false;
            }

            if($isTop && $isTop == "on"){
                $isTop = 1;
            }else{
                $isTop = 0;
            }

            $data['isTop'] = $isTop;
            $data['type'] = 3;
            if($add){
                $data['create_user'] = $_SESSION['think']['admin_user_auth']['uid'];
                $data['create_time'] =time();
                $res = Db::name('article')->insert($data);
            }else{
                $res = Db::name('article')->where(['id' => $id])->update($data);
            }

            if($res){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }

        }else{

            $id = $this->request->param('id/d');
            if($id){
                $data = Db::name('article')->where(['id' => $id])->find();
                if($data['pic']){
                    $data['pic'] = "/uploads/images/article/".$data['pic'];
                }
            }else{
                $data = [
                    'id' => '',
                    'title' => '',
                    'type' => '',
                    'info' => '',
                    'create_user' => '',
                    'create_time' => '',
                    'sort' => '',
                    'is_index' => '',
                    'pic' => '',
                    'text' => '',
                    'isTop' => ''
                ];
            }
            $this->assign('data',$data);

            return $this->fetch();
        }

    }


    /**
     * 文章删除
     * */
    public function del()
    {

        $id = input('id');

        if(!$id){
            $this->error('数据获取有误');
        }

        $data['status'] = -1;
        $res = Db::name('article')->where(['id' => $id])->update($data);

        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }

    }

}